using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Reflection;
using System.Text;

namespace yW5Lib
{
	public class clsVars : clsGenericBase<clsVar>
	{
		private clsXml cXml;

		public clsCollection<clsVar> colVars => base.colObjects;

		public clsVars()
		{
			cXml = new clsXml();
		}

		public bool WriteXML(ref StringBuilder SBP)
		{
			bool result = true;
			try
			{
				SBP.AppendLine("\t<PROJECTVARS>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = base.colObjects.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsVar clsVar = (clsVar)enumerator.Current;
						SBP.AppendLine(clsVar.DataAsXML());
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				SBP.AppendLine("\t</PROJECTVARS>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool CreateFromXML(string XMLData)
		{
			Clear();
			return UpdateFromXML(XMLData);
		}

		public void RemoveByName(string VarName)
		{
			for (int i = base.colObjects.Count(); i >= 1; i += -1)
			{
				if (Operators.CompareString(base.colObjects[i].Title, VarName, TextCompare: false) == 0)
				{
					base.colObjects.Remove(i);
				}
			}
		}

		public bool UpdateFromXML(string XMLData)
		{
			bool result = true;
			try
			{
				string[] DataArray = new string[2];
				cXml.ExtractXMLArray(XMLData, "<PROJECTVARS>", "</PROJECTVARS>", "</PROJECTVAR>", ref DataArray);
				int num = DataArray.Length - 1;
				for (int i = 0; i <= num; i++)
				{
					if (Strings.InStr(1, DataArray[i], "<PROJECTVAR>", CompareMethod.Text) > 0)
					{
						clsVar cObject = new clsVar();
						cObject.CreateFromXML(DataArray[i]);
						if (!Added(ref cObject))
						{
							clsShared.cLog.AddAction("Unable to add variable " + cObject.Title + " to project.");
							result = false;
						}
					}
				}
				DataArray = null;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}
	}
}
